/**
 * 发起申请列表页
 */
import React from 'react';
import { Card, Row, Col, Empty } from 'antd';
import { history, connect } from 'umi'
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import FlowIcon from '@/components/BizIcon/FlowIcon';
import PageRouter from '@/components/PageRouter';
import { withRoutePage } from '@/utils/enhanceUtils';
import authHelper from '@/utils/auth/helper';
import styles from './index.less';

const { checkCode } = authHelper;

const List = ({ mineList, dispatch, user }) => {
    const { list, categoryType } = mineList;

    const getCategoryName = code => {
        const find = categoryType.find(item => item.dictValue === code.toString());

        if (find) {
            return find.dictName;
        }
        return '';
    };

    const itemNeeToShow = filterItem => {
        // 如果是试用期才展示转正申请入口
        if (filterItem.type === 'regular' && user.currentUser.inServiceStatus !== '4') {
            return false;
        }
        return true;
    };

    const handleLinkTo = data => () => {
        const {
            templateId,
            processDeploymentId,
            formDeploymentId,
            templateName,
            processInstanceId,
            isOtherPost,
            type
        } = data;

        const LINK_INFO = {
            post: {
                pathname: '/personal/portal/flow/mine/applyPost', // 调岗申请
                query: { processTemplateId: processInstanceId, isOtherPost }
            },
            leave: {
                pathname: '/personal/portal/flow/mine/applyLeave', // 离职申请
                query: {
                    processTemplateId: processInstanceId,
                    otherPost: isOtherPost
                }
            },
            regular: {
                pathname: '/personal/portal/flow/mine/applyRegularization', // 转正申请
                query: {
                    processTemplateId: processInstanceId
                }
            }
        };

        const defaultLink = {
            pathname: '/personal/portal/flow/mine/list/apply', // 常规流程
            query: {
                templateId,
                processDeploymentId,
                formDeploymentId,
                templateName,
                id: ''
            }
        };

        history.push(LINK_INFO[type] || defaultLink);
    };

    React.useEffect(() => {
        dispatch({
            type: 'mineList/fetchCategoryType'
        });
        dispatch({
            type: 'mineList/fetchList',
            payload: {
                hasQuitHrAuth: checkCode('office:personnel:progress:leaveoffice:add')
            }
        });
    }, [dispatch]);

    return (
        <PageHeaderWrapper type="success">
            <PageRouter type="myflow">
                <div className={styles.list}>
                    {list && list.length > 0 ? (
                        list.map(item => (
                            <Card bordered={false} title={getCategoryName(item.category)} key={item.category}>
                                <Row type="flex" align="middle">
                                    {item.myApplyTemplateVOList
                                        .filter(filterItem => itemNeeToShow(filterItem))
                                        .map(subItem => (
                                            <Col
                                                title={subItem.templateName}
                                                key={subItem.templateId}
                                                onClick={handleLinkTo(subItem)}
                                            >
                                                <FlowIcon className={styles.icons} iconType={subItem.iconType} />
                                                <span className={styles.templateName}>{subItem.templateName}</span>
                                            </Col>
                                        ))}
                                </Row>
                            </Card>
                        ))
                    ) : (
                        <Card bordered={false}>
                            <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} />
                        </Card>
                    )}
                </div>
            </PageRouter>
        </PageHeaderWrapper>
    );
};

export default withRoutePage(connect(({ mineList, user }) => ({ mineList, user }))(List));
